<template>
    <div class="app-container">
        医院设置列表
        <!-- 查询表单 -->
        <el-form :inline="true" class="demo-form-inline">
               <el-form-item>
                <el-input  v-model="searchObj.hosname" placeholder="医院名称" />
            </el-form-item>
               <el-form-item>
                <el-input v-model="searchObj.hoscode" placeholder="医院编号" />    
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
            <el-button icon="el-icon-refresh" @click="reset()">重置</el-button>
        </el-form>

        <!-- 批量删除 -->
        <div>
            <el-button type="danger" size="mini" @click=batchRemove()>批量删除</el-button>
        </div>
        <!-- 表格主页 -->
        <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column prop="hosname" label="医院名称" width="150" />
            <el-table-column prop="hoscode" label="医院编号" />
            <el-table-column prop="apiUrl" label="api基础路径" width="150" />
            <el-table-column prop="contactsName" label="联系人姓名" width="120" />
            <el-table-column prop="contactsPhone" label="联系人手机" width="120" />
            <el-table-column label="状态" width="50" prop="status">
                <template slot-scope="scope">
                    <!-- "==="：三等于表示，类型和值均相等 -->
                    {{ scope.row.status === 1 ? '启用' : '禁用' }}
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="100">
                <template slot-scope="scope">
                    {{ scope.row.createTime|dateFormat }}
                </template>
            </el-table-column>
            <el-table-column prop="updateTime" label="更新时间" width="100">
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="danger" size="mini" @click="removeByID(scope.row.id)">删除
                    </el-button>
                    <!-- 路由 -->
                    <router-link :to="'/hospSet/edit/' + scope.row.id">
                        <!-- <el-button type="primary" size="mini" @click="update(scope.row.id)">编辑 -->
                        <el-button type="success" size="mini" >编辑
                        </el-button>
                    </router-link>
                    <el-button v-if=" scope.row.status==1" type="primary" size="mini" @click="lock(scope.row.id, 0)">启用
                    </el-button>
                    <el-button v-if="scope.row.status == 0" type="info" size="mini" @click="lock(scope.row.id, 1)">
                        禁用
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="total" @current-change="getList" :current-page.sync="curPage" :page-size="limit"
            layout="total, prev, pager, next, jumper" :total="total" style="padding: 30px; text-align: right;">
        </el-pagination>
    </div>


</template>

<script>
    import hospSet from '@/api/hospSet';

    export default {
        name: "HospSet",
        //定义变量和初始值
        data() {
            return {
                curPage: 1,
                limit: 5,
                total: 0,
                searchObj: {},  // 封装条件对象---JSON
                list: [],    // 每页数据---集合
                multiSelection: []  // 批量选中记录---列表
            }
        },
        //在页面渲染之前执行
        created() {
            // 调用method方法，返回数据
            this.getList()
        },

        // 定义：具体方法，对接接口
        methods: {
            getList(page = 1) {
                // 添加：”当前页“ 参数
                this.curPage = page;
                hospSet.getHospSetList(this.curPage, this.limit, this.searchObj)
                    .then(response => {
                        // console.log("res: ", response);
                        this.list = response.data.records;
                        this.total = response.data.total;
                    }).catch(error => {
                        // console.log("error", error);
                        alert(error);
                    });

            },
            //重置
            reset() {
                this.searchObj = {};
                this.getList();
            },
            //删除
            removeByID(id) {
                // alert(id);
                this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //调用接口
                    hospSet.delHospSet(id).then(res => {
                        //提示
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        // ---刷新页面---
                        this.getList();
                    })
                });
            },
            //批量删除
            handleSelectionChange(selection) {
                // console.log(selection);  ---复选框选中记录，吧【列表集合
                this.multiSelection = selection;
            },
            batchRemove() {
                var idList = [];
                for (var i = 0; i < this.multiSelection.length; i++) {
                    var obj = this.multiSelection[i];
                    var id = obj.id;
                    idList.push(id);
                }
                this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //调用接口
                    hospSet.batchDel(idList).then(res => {
                        //提示
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        // ---刷新页面---
                        this.getList();
                    })
                });
            },
            //更新状态
            lock(id, status) {
                hospSet.updateStatus(id, status).then(res => {
                    //---刷新页面记录---
                    this.getList();
                })
            },
        },

        //在页面渲染之后执行
        mounted() {

        }
    }

</script>


<style scoped>

</style>